package com.longrise.co.ui.mine.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.R;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.mine.contract.MineContract;
import com.longrise.co.ui.mine.model.MineModel;
import com.longrise.co.ui.mine.model.RxModifyBackData;
import com.longrise.co.ui.mine.presenter.MinePresenter;
import com.longrise.co.utils.Constants;
import com.ly.common.base.BaseFragment;
import com.ly.common.baserx.RxBus;
import com.ly.common.commonutils.StringUtils;

import butterknife.Bind;
import butterknife.OnClick;

import static com.longrise.co.ui.mine.fragment.MyProfileFragment.RX_MODIFY_NICK;

/**
 * Created by luoyang on 2017-7-20.
 */

public class ModifyNickNameFragment extends BaseFragment<MinePresenter, MineModel> implements MineContract.View<String> {

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.name)
    EditText name;

    private String mNickName;

    private boolean isSuccess = false;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_mine_nick;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        setText(headTitle, "我的昵称");
        mNickName = getArguments().getString("nickName");
    }

    public static ModifyNickNameFragment newInstance(String nickName) {
        
        Bundle args = new Bundle();
        args.putString("nickName", nickName);
        ModifyNickNameFragment fragment = new ModifyNickNameFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @OnClick({R.id.head_back, R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_back:
                _mActivity.onBackPressed();
                break;
            case R.id.confirm:
                if(StringUtils.isEmpty(name.getText().toString().trim())) {
                    showLongToast("修改昵称不能为空");
                    return ;
                }
                if(mNickName.equals(name.getText().toString().trim())) {
                    showLongToast("修改昵称与原昵称重复");
                    return ;
                }
                EntityBean bean = new EntityBean();
                bean.set("userflag", CommonPara.getInstance().getUserFlag());
                bean.set("nickname", name.getText().toString().trim());
                bean.set("user", CommonPara.getInstance().getUser());
                bean.set("sign", CommonPara.getInstance().getEnUserFlag());

                mPresenter.modifyUserInfo(bean);
                break;
        }
    }

    @Override
    public void requestSuccess(String s) {
        isSuccess = true;
        showLongToast("修改昵称成功！");
        RxModifyBackData data = new RxModifyBackData();
        data.type = RX_MODIFY_NICK;
        data.value = name.getText().toString().trim();
        RxBus.getInstance().post(Constants.RX_EVENT_MODIFY_NICK, data);
    }

    @Override
    public void onRequestComplete() {
        if(isSuccess) {
            _mActivity.onBackPressed();
        }
    }
}
